/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com" .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hh.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hh.droid.R;
import com.hh.listeners.OnHideBarListener;
import com.hh.listeners.OnNavItemsClickListener;
import com.hh.listeners.OnShowBarListener;


/**
 * 
 * An Horizontal List which use adapter to display data
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */
public class UiHorizontalNavBar extends LinearLayout implements OnClickListener {


	private View mConvertView;
	private Button _mBtnUp;
	private Button _mBtnDown;
	private LinearLayout _mNavBar;
	private FrameLayout _mFlBtnUp;
	private FrameLayout _mFlBtnDown;
	private UiHorizontalListView _mHListView;
	private OnNavItemsClickListener _mListener;
	private RelativeLayout _mCLickedZone;
	private OnShowBarListener _mShowListener;
	private OnHideBarListener _mHideListener;
	private boolean _mIsShowing;


	/**
	 * An Horizontal navigation bar with scroll feature and custom item
	 * @param context
	 * @param attrs
	 */
	public UiHorizontalNavBar(final Context context, AttributeSet attrs) {

		super(context, attrs);
	    
		mConvertView = LayoutInflater.from(context).inflate(R.layout.ui_hnavigation_barre, this);

		_mBtnUp = (Button) mConvertView.findViewById(R.id.NavBar_BtnUP);
		_mBtnUp.setOnClickListener(this);

		_mBtnDown = (Button) mConvertView.findViewById(R.id.NavBar_BtnDown);
		_mBtnDown.setOnClickListener(this);
		
		_mNavBar = (LinearLayout) mConvertView.findViewById(R.id.NavBar_SubContainer);
		
		_mFlBtnUp=(FrameLayout) findViewById(R.id.NavBar_FLBtnUp);
		_mFlBtnDown=(FrameLayout) findViewById(R.id.NavBar_FLBtnDown);
		
		_mHListView=(UiHorizontalListView) findViewById(R.id.NavBar_HListView);	
		
		_mCLickedZone=(RelativeLayout) findViewById(R.id.clickedZone);
		_mCLickedZone.setOnClickListener(this);
		
		_mHListView.setOnItemClickListener(new OnItemClickListener() {	
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				if(_mListener!=null)
					_mListener.onNavItemClick(arg0,arg1, arg2, arg3);
			}
		});
		
		/*
		 * Default color
		 */
		int lDefaultColor=getResources().getColor(R.color.holo_gray_lev0);
		
		_mHListView.setBackgroundColor(lDefaultColor);
		
		_mFlBtnUp.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_round_hor_navbar));
		_mFlBtnDown.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_round_hor_navbar));
		
		_mIsShowing=false;
	}
	
	/**
	 * <br>
	 * Set the background of the top button (button which open the view)
	 * 
	 * @author WajdiHh
	 * @param pDrawable :icon to set
	 */
	public void setBackgroundButtonUp(Drawable pDrawable){	
		_mFlBtnUp.setBackgroundDrawable(pDrawable);
		_mBtnUp.setBackgroundDrawable(null);
	}
	
	/**
	 * <br>
	 * Set the background of the bottom button (button which close the view)
	 * 
	 * @author WajdiHh
	 * @param pDrawable :icon to set
	 */
	public void setBackgroundButtonDown(Drawable pDrawable){	
		_mFlBtnDown.setBackgroundDrawable(pDrawable);
		_mBtnDown.setBackgroundDrawable(null);
	}
	/**
	 * <br>
	 * Set the background color of the navigation bar
	 * 
	 * @author WajdiHh
	 * @param pColor: icon to set
	 */	
	public void setBackgroundNavBar(int pColor){
		_mHListView.setBackgroundColor(pColor);
		
	}
	/**
	 * <br>
	 * Specify an alpha value for the drawable button up. 0 means fully transparent, and 255 means fully opaque
	 * 
	 * @author WajdiHh
	 * @param pAlpha 0 to 255
	 */
	public void setAlphaButtonUp(int pAlpha){
		_mFlBtnUp.getBackground().setAlpha(pAlpha);
	}
	
	/**
	 * <br>
	 * Specify an alpha value for the drawable  navigation bar. 0 means fully transparent, and 255 means fully opaque. 
	 * 
	 * @author WajdiHh
	 * @param pAlpha  0 to 255
	 */
	public void setAlphaNavBar(int pAlpha){
		_mHListView.getBackground().setAlpha(pAlpha);
		_mFlBtnDown.getBackground().setAlpha(pAlpha);
	}

	/**
	 * <br>
	 * Fix navigation bar width
	 * 
	 * @author WajdiHh
	 * @param pWidth :fixed width
	 */
	public void setWidth(int pWidth){

		RelativeLayout.LayoutParams params = 
				new RelativeLayout.LayoutParams(pWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);

		_mHListView.setLayoutParams(params);
	}

	/**
	 * <br>
	 * Fix navigation bar height
	 * 
	 * @author WajdiHh
	 * @param pHeight :fixed width
	 */
	public void setHeight(int pHeight){

		RelativeLayout.LayoutParams params = 
				new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,pHeight);

		_mHListView.setLayoutParams(params);
	}
	/**
	 * <br>
	 * Specify the adapter to display data
	 * 
	 * @author WajdiHh
	 * @param pAdapter : Adapter
	 */
	public void setAdapter(BaseAdapter pAdapter){
		
		if(pAdapter!=null){
			_mHListView.setAdapter(pAdapter);
		}
	}
	
	/**
	 * <br>
	 * Hide the navigation bar
	 *  @author WajdiHh
	 */
	public void hideBar(){
		
		Animation lAnimationHide=AnimationUtils.loadAnimation(getContext(),R.anim.push_down);
		_mNavBar.startAnimation(lAnimationHide);	
		lAnimationHide.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				_mNavBar.setVisibility(View.INVISIBLE);
			}	
			@Override
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				_mFlBtnUp.setVisibility(View.VISIBLE);	
				_mFlBtnUp.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.fadein));
				_mNavBar.setVisibility(View.GONE);				
			}
		});
		
		if(_mHideListener!=null)
			_mHideListener.onHideBar();
		
		_mIsShowing=false;
	}
	/**
	 * <br>
	 * show the navigation bar
	 *  @author WajdiHh
	 */
	public void showBar(){

		_mNavBar.setVisibility(View.VISIBLE);
		_mFlBtnUp.setVisibility(View.GONE);
		_mNavBar.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.push_up));
		
		if(_mShowListener!=null)
			_mShowListener.onShowBar();
		
		_mIsShowing=true;
	}

	/**
	 * <br>
	 * Check if navigation bar is Showing
	 * 
	 * @author WajdiHh
	 * @return  true : showing / false hiding
	 */
	public boolean isShowing(){
		return _mIsShowing;
	}
	
	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar is opened
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnShowBarListener
	 */
	public void setOnShowBarListener(OnShowBarListener pListener){
		_mShowListener=pListener;
	}

	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar is closed
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnHideBarListener
	 */
	public void setOnHideBarListener(OnHideBarListener pListener){
		_mHideListener=pListener;
	}

	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar item is clicked
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnNavItemsClickListener
	 */
	public void setOnNavItemsClickListener(OnNavItemsClickListener pListener){
		_mListener=pListener;
	}
	
	@Override
	public void onClick(View v) {

		if(v.getId()==R.id.NavBar_BtnUP){

			showBar();

		}else if(v.getId()==R.id.NavBar_BtnDown || v.getId()==R.id.clickedZone){

			hideBar();
		}
	}
}
